﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TiledLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MvdDonk.Bomberman
{
    public class Level : IDisposable
    {
        public Map Map { get; private set; }
        public TimeSpan TimeRemaining { get; private set; }
        public bool IsFinished { get; private set; }

        public Level(string mapName)
        {
            Map = Bomberman.Instance.ResourceManager.RequestMap("Maps/" + mapName);
            var metaLayer = Map.GetLayer("Meta");
            if (metaLayer == null)
                throw new Exception("Could not find a Meta layer in the map");
            metaLayer.Visible = false;
            IsFinished = false;
            TimeRemaining = TimeSpan.FromMinutes(2);
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Update(GameTime gameTime)
        {
            TimeRemaining -= TimeSpan.FromMilliseconds(gameTime.ElapsedGameTime.TotalMilliseconds);

            if (Bomberman.Instance.EntityManager.Players.Where(p => p.IsAlive).Count() <= 1)
                IsFinished = true;

            // Clamp the time remaining at zero.
            if (TimeRemaining < TimeSpan.Zero)
                TimeRemaining = TimeSpan.Zero;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Map.Draw(spriteBatch);
            Bomberman.Instance.EntityManager.Draw(gameTime, spriteBatch);
        }
    }
}
